package com.example.hp.controller;



import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import com.example.hp.entity.Courses;
import com.example.hp.redis.RedisCache;
import com.example.hp.service.impl.CoursesServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("redis")
public class redisDishController {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private CoursesServiceImpl coursesService;


    @GetMapping("/putObj")
    public ResponseEntity<Courses> putObj(@RequestParam Integer id) {
        // 从数据库获取课程对象
        Courses courses = coursesService.queryById(id);

        // 将课程对象转为 Map
        Map<String, Object> map = BeanUtil.beanToMap(courses);

        // 将 Map 存入 Redis 缓存，设置过期时间为 1 小时
        redisCache.setCacheObject(String.valueOf(courses.getCourseId()), map, 1, TimeUnit.HOURS);

        // 返回成功的响应，并包含课程对象
        return ResponseEntity.ok(courses);
    }


}